/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kmcsoft.erp;

import com.kmcsoft.core.service.MenuService;
import com.kmcsoft.core.ui.entity.Menu;
import com.kmcsoft.security.SecurityUtil;
import com.kmcsoft.spring.SpringUtil;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.A;
import org.zkoss.zul.Bandbox;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Comboitem;
import org.zkoss.zul.Div;
import org.zkoss.zul.Label;
import org.zkoss.zul.Toolbarbutton;
/**
 *
 * @author mylien
 */
public class MasterComposer extends GenericForwardComposer {

    protected Combobox cboTopMenu;

    protected Component subMenuBar;

    protected Component mainDiv, welcomeDiv, blankDiv, accessDenied;

    protected Bandbox masterMenu;

    @Autowired
    protected MenuService menuService;

    private Menu selectedMenu;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);

        menuService = SpringUtil.getBean("menuService", MenuService.class);

        String uri = (String) self.getAttribute("contentUri");

        if (uri == null) return;
        Menu current = menuService.getMenu(uri);

        if (current.getParent().isTopLevel()) {
            // Set default selected for 1 level menu
            for (Comboitem c : (List<Comboitem>) cboTopMenu.getItems()) {
                Menu topMenu = (Menu) c.getValue();

                if (current.getParent().getId() == topMenu.getId()) {

                    cboTopMenu.setSelectedItem(c);

                    // Render data
                    renderSubMenu(topMenu.getChildren());
                    return;
                }
            }
            masterMenu.setVisible(false);
        } else {
            renderSubMenu(menuService.getMenus(current.getParent()));
            for (Comboitem c : (List<Comboitem>) cboTopMenu.getItems()) {
                Menu topMenu = (Menu) c.getValue();

                if (topMenu.equals(current.getParent().getParent())) {
                    cboTopMenu.setSelectedItem(c);
                    selectedMenu = topMenu;
                    break;
                }
            }
            renderBandbox();
            masterMenu.setVisible(true);
        }
    }

    public void onChange$cboTopMenu() {
        self.setAttribute("contentUri", null);
        masterMenu.setVisible(false);
        setBlank();
        selectedMenu = (Menu) cboTopMenu.getSelectedItem().getValue();
        renderSubMenu(selectedMenu.getChildren());
    }

    public void onClick$changePassword() {
        Component win = execution.createComponents("/WEB-INF/inline/change-password.zul", null, null);
        win.setPage(page);
    }

    public List<Menu> getMasterMenu() {
//        return menuService.getMenus(menuService.getMenu(9));
        if (selectedMenu == null) {
            return Collections.EMPTY_LIST;
        }
        return menuService.getMenus(selectedMenu);
    }

    protected void renderBandbox() {
        
        List<Menu> masterMenus = getMasterMenu();
        
        if (masterMenus == null || masterMenus.isEmpty()) return;

        Component popup = masterMenu.getFirstChild();
        Components.removeAllChildren(popup);

        for (Menu menu : masterMenus) {

            if (SecurityUtil.isNoneGranted(menu.getAuthorities())) {
                continue;
            }

            Div div = new Div();
            div.setSclass("parent-menu");
            div.setParent(popup);

            Label mnuName = new Label(menu.getCaption());
            mnuName.setSclass("parent-menu");
            mnuName.setParent(div);

            Div childDiv = new Div();
            childDiv.setParent(div);
            childDiv.setSclass("child-menu");

            String uri = (String) self.getAttribute("contentUri");

            for (Menu menu1 : menu.getChildren()) {
                if (SecurityUtil.isNoneGranted(menu1.getAuthorities())) {
                    continue;
                }

                A a = new A(menu1.getCaption());
                if (menu1.getUri() != null && menu1.getUri().equals(uri)) {
                    a.setSclass("active");
                    masterMenu.setValue(menu.getCaption());
                } else {
                    a.setHref(menu1.getUri());
                }
                a.setParent(childDiv);
            }
        }
    }

    protected void renderSubMenu(List<Menu> children) {

        if (children == null || children.isEmpty()) {
            return;
        }

        String uri = (String) self.getAttribute("contentUri");
        
        // remove all current children
        Components.removeAllChildren(subMenuBar);

        List<Menu> menuChildren = menuService.getMenus(children.get(0));
        if (menuChildren != null && !menuChildren.isEmpty()) {
            renderBandbox();
            
            // Render first masterMenu
            Menu firsMaster = children.get(0);
            renderSubMenu(menuService.getMenus(firsMaster));
            masterMenu.setValue(firsMaster.getCaption());
            masterMenu.setVisible(true);
        } else {

//            masterMenu.setVisible(false);
            
            // Render new children
            for (Menu menu : children) {

                if (SecurityUtil.isNoneGranted(menu.getAuthorities())) {
                    continue;
                }

                Toolbarbutton tbBtn = new Toolbarbutton(menu.getCaption());
                if (menu.getUri() != null && menu.getUri().equals(uri)) {
                    tbBtn.setSclass("active");
                } else {
                    tbBtn.setHref(menu.getUri());
                }
                subMenuBar.appendChild(tbBtn);
            }
        }
    }

    protected void setBlank() {
        if (mainDiv != null) {
            mainDiv.setVisible(false);
        }
        if (welcomeDiv != null) {
            welcomeDiv.setVisible(false);
        }
        if (accessDenied != null) {
            accessDenied.setVisible(false);
        }
        blankDiv.setVisible(true);
    }
}
